﻿using YiShop.Framework.Extensions;
using YiShop.System.Repositories;

namespace YiShop.System.Services
{
    public class SysDeptService : ITransient
    {

        private readonly SysDeptRepository _sysDeptRepository;
        private readonly SysUserRepository _sysUserRepository;

        public SysDeptService(SysDeptRepository sysDeptRepository, SysUserRepository userRepository)
        {
            _sysDeptRepository = sysDeptRepository;
            _sysUserRepository = userRepository;
        }

        public async Task<List<SysDeptDto>> GetDtoListAsync(SysDeptDto dto)
        {
            return await _sysDeptRepository.GetDtoListAsync(dto);
        }

        #region TreeSelect
        /// <summary>
        /// 查询部门树结构信息
        /// </summary>
        public async Task<List<TreeSelect>> GetDeptTreeListAsync(SysDeptDto dto)
        {
            List<SysDeptDto> depts = await GetDtoListAsync(dto);
            return BuildDeptTreeSelect(depts);
        }

        /// <summary>
        /// 构建前端所需要下拉树结构
        /// </summary>
        private List<TreeSelect> BuildDeptTreeSelect(List<SysDeptDto> depts)
        {
            List<SysDeptDto> deptTrees = BuildDeptTree(depts);
            return deptTrees.Select(dept => new TreeSelect(dept)).ToList();
        }

        /// <summary>
        /// 构建前端所需要树结构
        /// </summary>
        private List<SysDeptDto> BuildDeptTree(List<SysDeptDto> depts)
        {
            List<SysDeptDto> returnList = new List<SysDeptDto>();
            List<long> tempList = depts.Where(d => d.DeptId.HasValue).Select(d => d.DeptId!.Value).ToList();
            foreach (SysDeptDto dept in depts)
            {
                // 如果是顶级节点, 遍历该父节点的所有子节点
                if (dept.ParentId.HasValue && !tempList.Contains(dept.ParentId.Value))
                {
                    RecursionFn(depts, dept);
                    returnList.Add(dept);
                }
            }
            if (returnList.IsEmpty())
            {
                returnList = depts;
            }
            return returnList;
        }

        /// <summary>
        /// 递归列表
        /// </summary>
        private void RecursionFn(List<SysDeptDto> list, SysDeptDto t)
        {
            // 得到子节点列表
            List<SysDeptDto> childList = GetChildList(list, t);
            t.Children = childList;
            foreach (SysDeptDto tChild in childList)
            {
                if (HasChild(list, tChild))
                {
                    RecursionFn(list, tChild);
                }
            }
        }

        /// <summary>
        /// 得到子节点列表
        /// </summary>
        private List<SysDeptDto> GetChildList(List<SysDeptDto> list, SysDeptDto t)
        {
            List<SysDeptDto> tList = new List<SysDeptDto>();
            foreach (SysDeptDto n in list)
            {
                if (n.ParentId > 0 && n.ParentId == t.DeptId)
                {
                    tList.Add(n);
                }
            }
            return tList;
        }

        /// <summary>
        /// 是否存在子节点
        /// </summary>
        /// <param name="deptId">部门ID</param>
        /// <returns></returns>
        public async Task<bool> HasChildByDeptIdAsync(long deptId)
        {
            return await _sysDeptRepository.HasChildByDeptIdAsync(deptId);
        }

        /// <summary>
        /// 查询部门是否存在用户
        /// </summary>
        /// <param name="deptId">部门ID</param>
        /// <returns></returns>
        public async Task<bool> CheckDeptExistUserAsync(long deptId)
        {
            return await _sysUserRepository.CheckDeptExistUserAsync(deptId);
        }

        private bool HasChild(List<SysDeptDto> list, SysDeptDto t)
        {
            return GetChildList(list, t).Count > 0;
        }

        #endregion
    }
}
